﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Core.Trophy
{
    public class Trophy
    {
        public static List<Model.Badge> GetUserTrophies(int userId,ref Model.HangoutDBEntities context)
        {
            List<Model.Badge> badge= context.UserBadges.Where(o => o.UserID==userId).Select(o => o.Badge).ToList();

            return UpdateServerBadgePicURL(badge);
        }

        public static List<Model.Badge> UpdateServerBadgePicURL(List<Model.Badge> badges)
        {
            List<Model.Badge> b = new List<Model.Badge>();
            foreach (Model.Badge badge in badges)
            {
                badge.BadgePic = Settings.Root+Settings.TrophyImages+badge.BadgePic;
                b.Add(badge);
            }

            return b;
        }


        public static List<Model.Badge> CheckBadgeAwarded(int userId, ref Model.HangoutDBEntities context)
        {

            List<Model.Badge> badge = new List<Model.Badge>();
            //get number of hangouts 
            int noOfHangouts = Core.Meetup.Meetup.GetNoOfCheckIns(userId, context);
            //get max no of people in a hangout
            int maxPeople = Core.Meetup.Meetup.GetMaxNoOfPeopleInUserCheckins(userId,context);
            //Did the user hangout at midnight
            //bool midnightHangout = Core.Meetup.Meetup.HangoutAtMidnight(userId, context);

            if (noOfHangouts >= 5&&!HasFiveHangoutTrophy(userId,context))
            {
                //award five hangout trophy here :)
                badge.Add(AwardTrophy(userId, Badge.FiveHangouts,ref context));
            }

            if (noOfHangouts >= 10 && !HasTenHangoutTrophy(userId, context))
            {
                badge.Add(AwardTrophy(userId, Badge.TenHangouts, ref context));
            }

            if (maxPeople >= 5 && !HasGangOfFiveTrophy(userId, context))
            {
                badge.Add(AwardTrophy(userId, Badge.GangOfFive, ref context));
            }

            if (maxPeople >= 10 && !HasGangOfTenTrophy(userId, context))
            {
                badge.Add(AwardTrophy(userId, Badge.GangOfTen, ref context));
            }

            //if (midnightHangout && !HasMidnightTrophy(userId, context))
            //{
            //    badge.Add(AwardTrophy(userId, Badge.NightOut, ref context));
            //}

            return badge;

        }

        public static bool HasMidnightTrophy(int userId,Model.HangoutDBEntities context)
        {
            return HasTrophy(userId, (int)Badge.NightOut, context);
        }

        public static bool HasFiveHangoutTrophy(int userId, Model.HangoutDBEntities context)
        {
            return HasTrophy(userId, (int)Badge.FiveHangouts, context);
        }

        public static bool HasTenHangoutTrophy(int userId, Model.HangoutDBEntities context)
        {
            return HasTrophy(userId, (int)Badge.TenHangouts, context);
        }

        public static bool HasGangOfFiveTrophy(int userId, Model.HangoutDBEntities context)
        {
            return HasTrophy(userId, (int)Badge.GangOfFive, context);
        }

        public static bool HasGangOfTenTrophy(int userId, Model.HangoutDBEntities context)
        {
            return HasTrophy(userId,(int)Badge.GangOfTen, context);
        }



        public static bool HasTrophy(int userid, int trophyId, Model.HangoutDBEntities context)
        {
            if (context.UserBadges.Where(o => o.UserID == userid && o.BadgeID == trophyId).Count() > 0)
            {
                return true;
            }

            return false;
        }


        public static Model.Badge AwardTrophy(int userId,Badge badge,ref Model.HangoutDBEntities context)
        {
            if(HasTrophy(userId,(int)badge,context))
            {
                return null;
            }

            Model.UserBadge ub = new Model.UserBadge();
            ub.BadgeID = (int)badge;
            ub.DateTimeStamp = DateTime.Now;
            ub.UserID = userId;

            context.AddToUserBadges(ub);
            context.SaveChanges();

            return GetTrophy((int)badge,context);

        }

        public static Model.Badge GetTrophy(int trophyId,Model.HangoutDBEntities context)
        {
            return context.Badges.Where(o => o.BadgeID == trophyId).FirstOrDefault();
        }

        
    }
}